package com.lenovo.smarttraffic.ui.activity.stage6;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lenovo.smarttraffic.MainActivity;
import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.bean.ParkBean;
import com.lenovo.smarttraffic.ui.activity.BaseActivity;
import com.lenovo.smarttraffic.util.HttpUtils;
import com.lenovo.smarttraffic.util.ToastUtils;
import com.lenovo.smarttraffic.util.Utils;

import java.lang.ref.WeakReference;
import java.util.List;

public class ParkingActivity extends BaseActivity {

    public static WeakReference<ParkingActivity> activity;
    public static List<ParkBean.ROWSDETAILBean> list;
    public static int pos = 0;

    private Toolbar toolbar;
    private TextView toolbar_title;
    private LinearLayout icon_parent;
    private ImageView settings, history;

    private LinearLayout loc_parent;
    private ListView park_lv;

    private LinearLayout info_parent;
    private TextView name_tv;
    private View type_ic;
    private TextView type_tv, loc_tv, distant_tv, money_tv, count_tv, money_tv2;

    @Override
    protected int getLayout() {
        return R.layout.activity_parking;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        activity = new WeakReference<>(this);

        toolbar = findViewById(R.id.toolbar);
        toolbar.setNavigationOnClickListener(v -> {
            if (info_parent.getVisibility() == View.VISIBLE) {
                toggleView();
            } else finish();
        });
        toolbar_title = findViewById(R.id.toolbar_title);
        icon_parent = findViewById(R.id.icon_parent);
        settings = findViewById(R.id.settings);
        history = findViewById(R.id.history);

        loc_parent = findViewById(R.id.loc_parent);
        park_lv = findViewById(R.id.park_lv);

        info_parent = findViewById(R.id.info_parent);
        name_tv = findViewById(R.id.name_tv);
        type_ic = findViewById(R.id.type_ic);
        type_tv = findViewById(R.id.type_tv);
        loc_tv = findViewById(R.id.loc_tv);
        distant_tv = findViewById(R.id.distant_tv);
        money_tv = findViewById(R.id.money_tv);
        count_tv = findViewById(R.id.count_tv);
        money_tv2 = findViewById(R.id.money_tv2);

        if (MainActivity.userName.equals("admin")) {
            icon_parent.setVisibility(View.VISIBLE);
        }
        settings.setOnClickListener((v) -> {
            View view = View.inflate(this, R.layout.dialog_park_set, null);
            TextView money_tv = view.findViewById(R.id.money_tv);
            Spinner type_sp = view.findViewById(R.id.type_sp);
            EditText money_et = view.findViewById(R.id.money_et);
            Button set_btn = view.findViewById(R.id.set_btn);
            Button cancel_btn = view.findViewById(R.id.cancel_btn);
            AlertDialog ad = new AlertDialog.Builder(this)
                    .setView(view)
                    .show();

            set_btn.setOnClickListener(v1 -> {
                int money = Utils.parseInt(money_et.getText().toString());
                if (money < 1 || money > 999) {
                    ToastUtils.showToast(this, "请输入正确的值");
                } else {
                    HttpUtils.post("set_rate", new HttpUtils().add("UserName", MainActivity.userName).add("payment_method", type_sp.getSelectedItemPosition() == 0 ? "time" : "count").add("rate_sum", money + ""),
                            response -> {
                                if (Utils.getResult(response)) {
                                    ad.dismiss();
                                    ToastUtils.showToast(this, "设置成功");
                                } else {
                                    ToastUtils.showToast(this, "设置失败");
                                }
                            });
                }
            });
            cancel_btn.setOnClickListener(v12 -> ad.dismiss());
        });
        history.setOnClickListener(v -> startActivity(new Intent(this,ParkHistoryActivity.class)));

        HttpUtils.post("get_park", new HttpUtils().add("UserName", MainActivity.userName), response -> {
            list = new Gson().fromJson(response.toString(), ParkBean.class).getROWS_DETAIL();
            park_lv.setAdapter(new ParkAdapter());
        });
    }

    public void toggleView() {
        if (loc_parent.getVisibility() == View.VISIBLE) {
            toolbar_title.setText("停车场详情");
            loc_parent.setVisibility(View.GONE);
            info_parent.setVisibility(View.VISIBLE);
            icon_parent.setVisibility(View.GONE);

            ParkBean.ROWSDETAILBean bean = list.get(pos);
            name_tv.setText(bean.getParking_name());
            type_ic.setBackgroundResource(bean.getOpen_type().equals("对外开放") ? R.mipmap.status_open : R.mipmap.status_close);
            type_tv.setText(bean.getOpen_type());
            loc_tv.setText(bean.getParking_address());
            distant_tv.setText(bean.getDistance() + "米");

            int moneyPerHour = Utils.parseInt(bean.getCharging_reference().substring(3, bean.getCharging_reference().length() - 1));
            money_tv.setText(moneyPerHour + "元/小时");
            count_tv.setText(bean.getFree_parking_number() + "个/" + bean.getTotal_parking_number());
            money_tv2.setText(bean.getCharging_reference() + "，最高" + bean.getPayment_method() + "元/天");
        } else {
            toolbar_title.setText("停车场");
            info_parent.setVisibility(View.GONE);
            loc_parent.setVisibility(View.VISIBLE);
            icon_parent.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onBackPressedSupport() {
        if (info_parent.getVisibility() == View.VISIBLE) {
            toggleView();
        } else super.onBackPressedSupport();
    }
}

class ParkAdapter extends BaseAdapter {

    private List<ParkBean.ROWSDETAILBean> data = ParkingActivity.list;

    public ParkAdapter() {
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_parking, parent, false);
            holder.id_tv = convertView.findViewById(R.id.id_tv);
            holder.info_tv = convertView.findViewById(R.id.info_tv);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        ParkBean.ROWSDETAILBean bean = data.get(position);
        holder.id_tv.setText("" + bean.getId());
        holder.info_tv.setText(bean.getParking_name() + "\n" +
                "空车位" + bean.getFree_parking_number() + "个 | " + "停车费" + bean.getCharging_reference().substring(3, bean.getCharging_reference().length() - 1) + "元/小时\n" + bean.getDistance() + "米|" + bean.getParking_address());

        convertView.setOnClickListener(v -> {
            ParkingActivity.pos = position;
            ParkingActivity.activity.get().toggleView();
        });

        return convertView;
    }

    static class ViewHolder {
        TextView id_tv, info_tv;
    }
}
